package cn.edu.neusoft.maxingxin231.yuejian06.Activity;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cn.edu.neusoft.maxingxin231.yuejian06.Bean.AttentionBean;
import cn.edu.neusoft.maxingxin231.yuejian06.Bean.RegisterBean;
import cn.edu.neusoft.maxingxin231.yuejian06.Fragment.ClubCoachFragment;
import cn.edu.neusoft.maxingxin231.yuejian06.Fragment.ClubCourseFragment;
import cn.edu.neusoft.maxingxin231.yuejian06.Fragment.ClubVideoFragment;
import cn.edu.neusoft.maxingxin231.yuejian06.Listener.LoginListener;
import cn.edu.neusoft.maxingxin231.yuejian06.Model.AttClubModel;
import cn.edu.neusoft.maxingxin231.yuejian06.R;
// TrainFragment.OnFragmentInteractionListener,
public class ClubDetail extends BaseActivity implements View.OnClickListener,
    ClubCoachFragment.OnFragmentInteractionListener,
        ClubCourseFragment.OnFragmentInteractionListener,
        ClubVideoFragment.OnFragmentInteractionListener {
    String club_id,club_name,club_intro,club_img,userid,club_address;
    ImageButton bt_back,bt_att;
    TextView tv_title,tv_clubintro,tv_cname,tv_jianjie,tv_add;
    ImageView banner_img;
    LinearLayout tab_course,tab_video,tab_coach;
    ImageView img_course,img_video,img_coach;
    ClubCoachFragment fragment_club_coach;
    ClubCourseFragment fragment_club_course;
    ClubVideoFragment fragment_club_video;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    AttClubModel attClubModel = new AttClubModel();

    String user_id,flag;

    LoginListener<AttentionBean> attentionBeanLoginListener = new LoginListener<AttentionBean>() {
        @Override
        public void onResponse(AttentionBean attentionBean) {
            flag = attentionBean.getSuccess();
            if (flag.equals("1")){
                bt_att.setImageResource(R.drawable.ic_collect1);
            }
            else{
                bt_att.setImageResource(R.drawable.ic_collect);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    LoginListener<AttentionBean> tListener = new LoginListener<AttentionBean>() {
        @Override
        public void onResponse(AttentionBean attentionBean) {
            String a = attentionBean.getSuccess();
            if (a.equals("1")) {
                //刷新页面 重新调用一次方法
                isAttention();
                Toast.makeText(ClubDetail.this, "取消关注成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(ClubDetail.this, "取消关注失败1", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
    LoginListener<AttentionBean> aListener = new LoginListener<AttentionBean>() {
        @Override
        public void onResponse(AttentionBean attentionBean) {
            String a = attentionBean.getSuccess();
            if (a.equals("1")){
                isAttention();
                Toast.makeText(ClubDetail.this, "关注成功", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(ClubDetail.this, "关注失败", Toast.LENGTH_SHORT).show();
            }
//            if (attention!="0"){
//                bt_att.setImageResource(R.drawable.ic_collect1);
//                showToast("attention");
//            }
//            else {
//                bt_att.setImageResource(R.drawable.ic_collect);
//                showToast("unattention");
//            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ClubDetail.this,msg,Toast.LENGTH_SHORT).show();
        }
    };





    @Override
    void initView() {
        setLayout_file(R.layout.activity_club_detail);
        bt_back = (ImageButton)findViewById(R.id.bt_back);
        bt_att =(ImageButton)findViewById(R.id.bt_att);
        tv_title = (TextView)findViewById(R.id.tv_title);
        tv_cname = (TextView)findViewById(R.id.tv_cname);
        tv_jianjie = (TextView)findViewById(R.id.tv_jianjie);
        tv_clubintro = (TextView)findViewById(R.id.tv_clubintro);
        tv_add =(TextView)findViewById(R.id.tv_add);
        banner_img = (ImageView)findViewById(R.id.banner_img);

        club_id = getIntent().getStringExtra("club_id");
        club_name = getIntent().getStringExtra("club_name");
        club_intro = getIntent().getStringExtra("club_intro");
        club_img = getIntent().getStringExtra("club_pic");
        club_address = getIntent().getStringExtra("club_address");
        tv_cname.setText(club_name);
        tv_clubintro.setText(club_intro);
        tv_add.setText(club_address);
        Picasso.with(ClubDetail.this).load("http://10.0.2.2:80/yuejian14/public/static/club_img/"+club_img).into(banner_img);
        showToast(club_id);
       tab_video = (LinearLayout)findViewById(R.id.tab_video);
       tab_coach = (LinearLayout)findViewById(R.id.tab_coach);
       tab_course = (LinearLayout)findViewById(R.id.tab_course);
       img_video = (ImageView)findViewById(R.id.img_video);
        img_coach = (ImageView)findViewById(R.id.img_coach);
        img_course = (ImageView)findViewById(R.id.img_course);
        setSelect(0);
        isAttention();

    }



    @Override
    void initEvent() {
        tab_coach.setOnClickListener(this);
        tab_course.setOnClickListener(this);
        tab_video.setOnClickListener(this);


        //返回
        bt_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //关注
        bt_att.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag.equals("1")){
                    attClubModel.unattclub(getUser_id(),club_id,tListener);
                }else{
                    attClubModel.attclub(getUser_id(),club_id,aListener);
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        resetImage();
        switch (v.getId()){
            case R.id.tab_coach:
                setSelect(0);
                break;
            case R.id.tab_course:
                setSelect(1);
                break;
            case R.id.tab_video:
                setSelect(2);
                break;
             default:
                 break;
        }
    }
    private void resetImage() {
        img_coach.setImageResource(R.drawable.ic_club_coach);
        img_course.setImageResource(R.drawable.ic_club_course);
        img_video.setImageResource(R.drawable.ic_club_video);
    }
    private void setSelect(int i) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (i){
            case 0:
                if (fragment_club_coach == null){
                    fragment_club_coach = new ClubCoachFragment();

                    Bundle bundle = new Bundle();
                    bundle.putString("club_id",club_id);
                    fragment_club_coach.setArguments(bundle);

                    fragmentTransaction.add(R.id.content,fragment_club_coach);

                }
                else {
                    fragmentTransaction.show(fragment_club_coach);
                }
                img_coach.setImageResource(R.drawable.ic_club_coach1);
                break;
            case 1:
                if (fragment_club_course == null){
                    fragment_club_course = new ClubCourseFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("club_id",club_id);
                    fragment_club_course.setArguments(bundle);
                    fragmentTransaction.add(R.id.content,fragment_club_course);
                }
                else {
                    fragmentTransaction.show(fragment_club_course);
                }
                img_course.setImageResource(R.drawable.ic_club_course1);
                break;
            case 2:
                if (fragment_club_video == null){
                    fragment_club_video = new ClubVideoFragment();
                    Bundle bundle=new Bundle();
                    bundle.putString("club_id",club_id);
                    fragment_club_video.setArguments(bundle);
                    fragmentTransaction.add(R.id.content,fragment_club_video);
                }
                else {
                    fragmentTransaction.show(fragment_club_video);
                }
                img_video.setImageResource(R.drawable.ic_club_video1);
                break;
             default:break;
        }
        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (fragment_club_coach!=null){
            fragmentTransaction.hide(fragment_club_coach);
        }
        if (fragment_club_course!=null){
            fragmentTransaction.hide(fragment_club_course);
        }
        if (fragment_club_video!=null){
            fragmentTransaction.hide(fragment_club_video);
        }

    }



    @Override
    void initData() {

    }

    void isAttention(){
        AttClubModel isAttClubModel = new AttClubModel();
        isAttClubModel.isattclub(getUser_id(),club_id,attentionBeanLoginListener);
    }

//    void setImgattention(){
//        AttClubModel attClubModel = new AttClubModel();
//        attClubModel.attclub(getUser_id(),club_id,registerBeanLoginListener);
//        attClubModel.unattclub(getUser_id(),club_id,registerBeanLoginListener);
//    }


    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {


    }


    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}
